<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of tahunajaran
 *
 * @author miyzan
 */
class tahunajaran extends MY_Controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model("tahunajaran_model", "mth");
        $this->load->library('../controllers/global_reference');
    }

    public function get_rows() {

        $page = ($this->input->post('page', TRUE) ? $this->input->post('page', TRUE) : 1);
        $start = isset($_POST['start']) ? $this->db->escape_str($this->input->post('start', TRUE)) : 0;
        $limit = isset($_POST['limit']) ? $this->db->escape_str($this->input->post('limit', TRUE)) : $this->config->item("length_records");
        $search = isset($_POST['query']) ? $this->db->escape_str($this->input->post('query', TRUE)) : '';

        $result = $this->mth->get_rows($search, $start, $limit);
        echo $result;
    }
    
    public function get_rows_combo() {        

        $result = $this->mth->get_rows_combo();
        echo $result;
    }

    public function get_rows_detail() {

        $page = ($this->input->post('page', TRUE) ? $this->input->post('page', TRUE) : 1);
        $start = isset($_POST['start']) ? $this->db->escape_str($this->input->post('start', TRUE)) : 0;
        $limit = isset($_POST['limit']) ? $this->db->escape_str($this->input->post('limit', TRUE)) : $this->config->item("length_records");
        $search = isset($_POST['query']) ? $this->db->escape_str($this->input->post('query', TRUE)) : '';

        $result = $this->mth->get_rows_detail($search, $start, $limit);
        echo $result;
    }

    public function update_rows() {

        $opt = isset($_POST['cmd']) ? $this->db->escape_str($this->input->post('cmd', TRUE)) : '';
        $id = isset($_POST['id_th_ajaran']) ? $this->db->escape_str($this->input->post('id_th_ajaran', TRUE)) : '';
        $th = isset($_POST['tahun_ajaran']) ? $this->db->escape_str($this->input->post('tahun_ajaran', TRUE)) : '';
        $aktif = isset($_POST['mth_aktif']) ? $this->db->escape_str($this->input->post('mth_aktif', TRUE)) : FALSE;
        $detail = isset($_POST['postdata']) ? json_decode($this->input->post('postdata', TRUE)) : array();
        if ($aktif == 'on') {
            $aktif = 1;
        } else {
            $aktif = 0;
        }
        if ($opt == 'insert') {

//            $gr=new global_reference();
            $kode = 'TA';
            $rkode = $this->global_reference->sequence($kode);
            $id = $kode . '-' . $rkode[0]->retval;
        }
        $param = array($opt, $id, $th, $aktif);
        $spname = 'sp_master_thajaran';
        $result = $this->mth->SP_execData($spname, $param, true);
        //delete deteail
        if ($opt == 'insert' || $opt == 'update') {
            $param = array('deleteall', $id, '', 0);
            $spname = 'sp_detail_thajaran';
            $result = $this->mth->SP_execData($spname, $param, true);
            foreach ($detail as $dt) {
                if ($dt->aktif == 'on') {
                    $dt->aktif = 1;
                } else {
                    $dt->aktif = 0;
                }
                $param = array('insert', $id, $dt->semester, $dt->aktif);
                $spname = 'sp_detail_thajaran';
                $result = $this->mth->SP_execData($spname, $param, true);
            }
        }
        echo $result;
    }
    public function setaktif() {

        $opt = isset($_POST['cmd']) ? $this->db->escape_str($this->input->post('cmd', TRUE)) : '';
        $data = isset($_POST['postdata']) ? json_decode($this->input->post('postdata', TRUE)) : array();
        $id = $data->id_th_ajaran;
        $th = $data->tahun_ajaran;
        $aktif = $data->aktif;
        
        if ($aktif == 'on') {
            $aktif = 1;
        } else {
            $aktif = 0;
        }
        
        $param = array($opt, $id, $th, $aktif);
        $spname = 'sp_master_thajaran';
        $result = $this->mth->SP_execData($spname, $param, true);
        echo $result;
    }
    public function setaktif_detail() {
        $opt = isset($_POST['cmd']) ? $this->db->escape_str($this->input->post('cmd', TRUE)) : '';
        $data = isset($_POST['postdata']) ? json_decode($this->input->post('postdata', TRUE)): array();         
         $id = $data->id_th_ajaran;
        $th = $data->semester;
        $aktif = $data->aktif;
        if ($aktif == 'on') {
            $aktif = 1;
        } else {
            $aktif = 0;
        }
        $param = array($opt, $id, $th, $aktif);
        $spname = 'sp_detail_thajaran';
        $result = $this->mth->SP_execData($spname, $param, true);
       
        echo $result;
    }

    public function delete_row() {
        $opt = isset($_POST['cmd']) ? $this->db->escape_str($this->input->post('cmd', TRUE)) : '';
        $data = isset($_POST['postdata']) ? json_decode($this->input->post('postdata', TRUE)) : array();
        $id = $data->id_th_ajaran;
        $th = $data->tahun_ajaran;
        $aktif = $data->aktif;

        $param = array($opt, $id, $th, $aktif);
        $spname = 'sp_master_thajaran';
        $result = $this->mth->SP_execData($spname, $param, true);

        $param = array('deleteall', $id, '', 0);
        $spname = 'sp_detail_thajaran';
        $resultdel = $this->mth->SP_execData($spname, $param, true);

        echo $result;
    }

//    public function update_rows_detail() {
//        $opt = isset($_POST['cmd']) ? $this->db->escape_str($this->input->post('cmd', TRUE)) : '';
//        $id = isset($_POST['id_th_ajaran']) ? $this->db->escape_str($this->input->post('id_th_ajaran', TRUE)) : '';
//        $detail = isset($_POST['postdata']) ? $this->db->escape_str($this->input->post('postdata', TRUE)) : '';
//
//        foreach ($detail as $dt) {
//            $param = array($opt, $id, $dt->semester, $dt->aktif);
//            $spname = 'sp_detail_thajaran';
//            $result = $this->mth->SP_execData($spname, $param, true);
//        }
//
//
//        echo $result;
//    }

    public function delete_row_detail() {
        $opt = isset($_POST['cmd']) ? $this->db->escape_str($this->input->post('cmd', TRUE)) : '';
        $data = isset($_POST['postdata']) ? json_decode($this->input->post('postdata', TRUE)) : array();
        $id = $data->id_th_ajaran;
        $th = $data->semester;
        $aktif = $data->aktif;
        if ($aktif == 'on' || $aktif == 'true') {
            $aktif = 1;
        } else {
            $aktif = 0;
        }
        
            $param = array($opt, $id, $th, $aktif);
            $spname = 'sp_detail_thajaran';
            $result = $this->mth->SP_execData($spname, $param, true);
       
        echo $result;
    }

}

?>
